
import { useState, useCallback } from 'react';

type SetStateAction<S> = Partial<S> | ((prevState: S) => Partial<S>);

function useSetState<S extends Record<string, any>>(initialState: S) {
  const [state, setState] = useState<S>(initialState);

  const setMergedState = useCallback((newState: SetStateAction<S>) => {
    setState(prevState => ({
      ...prevState,
      ...(typeof newState === 'function' ? newState(prevState) : newState),
    }));
  }, []);

  return [state, setMergedState] as const;
}

export default useSetState